package yxy.game.pm2.command.shop;

import yxy.cherry.data.DataCenter;
import yxy.cherry.data.bean.unionshop;
import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.bean.PlayerBean;
import yxy.game.pm2.bean.db.dao.MySQLServerDAO;
import yxy.game.pm2.bean.db.module.server;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.ShopMessage;
import yxy.game.pm2.module.player.FunctionHandle;
import yxy.game.pm2.module.shop.handle.CreditUnionShopHandle;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/** 商店 积分联盟商店 */
@MessageType(proto = Command.CreditUnionShop, classtype = ShopMessage.CreditUnionShopRequest.class)
public class CMD_CreditUnionShop extends Command<ShopMessage.CreditUnionShopRequest> {

	@Override
	public void execute(MessageSource messageSource, ShopMessage.CreditUnionShopRequest request) {
		try {
			String playerCode = messageSource.getTag();

			ShopMessage.CreditUnionShopResponse.Builder response = ShopMessage.CreditUnionShopResponse.newBuilder();

			//根据开服时间和showtime过滤商品是否展示
			Collection<ShopMessage.Goods> goods = checkShopItemIsShow(playerCode);

			response.addAllGoods(goods);

			send(messageSource, response, Result.Success);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	//根据开服时间和showtime过滤商品是否展示
	private Collection<ShopMessage.Goods> checkShopItemIsShow(String playerCode) {
		Collection<ShopMessage.Goods> goods = new CreditUnionShopHandle().goods(playerCode);
		try {
			//商品表信息
			Map<String, unionshop> datas = DataCenter.getAllData(unionshop.class);
			ArrayList<String> idList = new ArrayList<>();
			//个人的商品信息
			//根据区id获取开服信息,计算已开服日期
			Long alreadlyPassTime = FunctionHandle.getAlreadlyPassTime(playerCode);
			//获取不展示的商品id集合
			Set<Map.Entry<String, unionshop>> entries = datas.entrySet();
			for (Map.Entry<String, unionshop> entry : entries) {
				int showtime = entry.getValue().getShowtime();
				//showtime不等于0且  已开服时间大于showtime才展示商品
				if (alreadlyPassTime - showtime < 0 || showtime == -1) {
					idList.add(entry.getKey());
				}
			}

			//移除对应的不展示商品
			Iterator<ShopMessage.Goods> iterator = goods.iterator();
			while (iterator.hasNext()) {
				ShopMessage.Goods good = iterator.next();
				int id = good.getId();
				if(idList.contains(String.valueOf(id))){
					iterator.remove();
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		return goods;
	}

	public Long getAlreadlyPassTime(String playerCode) throws ParseException {
		//用户信息
		PlayerBean playerBean = PlayerBean.get(playerCode);
		//服id
		int serverId = playerBean.getServerId();
		//区服信息
		server server = MySQLServerDAO.getFromDAOAccountManager(serverId);
		//开服日期
		Date openTime = server.getOpenTime();
		//当前日期
		Date nowTime = new Date();
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String format1 = simpleDateFormat.format(nowTime);
		String format2 = simpleDateFormat.format(openTime);
		Date now = simpleDateFormat.parse(format1);
		Date before = simpleDateFormat.parse(format2);
		long sub = Math.abs(now.getTime() - before.getTime());
		sub = sub / 1000 / 60 / 60 / 24;
		return sub;
	}



	private void send(MessageSource messageSource, ShopMessage.CreditUnionShopResponse.Builder response, Result result) {
		response.setResult(result.packResult());
		MessageEmitterHandler.getInstance().send(messageSource, response.build());
	}
}